home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / strobj.zip / MPSTROBJ.PAS < prev    next >
Pascal/Delphi Source File  |  1993-01-04  |  16KB  |  462 lines

  1. {$A+,B-,V+}
  2. {$M 16384,0,655360}
  3.  
  4. {.HI}
  5.  
  6. UNIT MpStrObj;
  7.  
  8. {
  9.  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  10.  * Unit.....: MpStrObj                                                       *
  11.  * Function.: Unit to create a String Object with methods for the object.    *
  12.  * Author...: Mark C. Paxton                                                 *
  13.  * Date.....: June 27, 1989                                                  *
  14.  * Test File: TSTROBJ.PAS (This file executes several tests of this unit.)   *
  15.  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  16. }
  17.  
  18. (*
  19.  
  20.            Short Description of the methods in MpStrObj.PAS Unit
  21.  
  22.  
  23.   MpStrObj is a  unit that implements a String  Object with several methods
  24.   for manipulating the Object. Each of the methods is documented below.
  25.  
  26.  
  27. ---------------------------------------------------------------------------
  28. FUNCTION CountOccurencesOfChar(CaseInsensitive:Boolean;C:Char):Integer;
  29. ---------------------------------------------------------------------------
  30.  
  31.   This method returns a count of the number of time it finds character C in
  32.   the  String Object.  CaseInsensitive is  a Boolean  indicator to  control
  33.   whether the count  is case sensitive or not.  If CaseInsensitive is TRUE,
  34.   then both  the lower and  upper case occurrences  of a character  will be
  35.   counted as a match.
  36.  
  37. ---------------------------------------------------------------------------
  38. FUNCTION Len:Integer;
  39. ---------------------------------------------------------------------------
  40.  
  41.   This method returns the length of the current string object.
  42.  
  43. ---------------------------------------------------------------------------
  44. FUNCTION PositionWhere(aString2:String):Integer;
  45. ---------------------------------------------------------------------------
  46.  
  47.   This  method  implements  a case insensitive POS function  for the String
  48.   Object.
  49.  
  50. ---------------------------------------------------------------------------
  51. FUNCTION Show:String;
  52. ---------------------------------------------------------------------------
  53.  
  54.   This method simply shows or returns  the value of the string object. This
  55.   is the counterpart to the Assign method.
  56.  
  57. ---------------------------------------------------------------------------
  58. PROCEDURE Assign(S:String);
  59. ---------------------------------------------------------------------------
  60.  
  61.   This method assigns  a new value to the string  object. See also the Show
  62.   method.
  63.  
  64. ---------------------------------------------------------------------------
  65. PROCEDURE ConstructStringOf(C:Char; N:Integer);
  66. ---------------------------------------------------------------------------
  67.  
  68.   This method assigns the C character N times to the string object.
  69.  
  70.   For example:
  71.  
  72.         Var aString : StringObject;
  73.         ...
  74.         begin
  75.           aString.ConstructStringOf('-',14);
  76.  
  77.   The above assigns 14 -'s to the string object.
  78.  
  79. ---------------------------------------------------------------------------
  80. PROCEDURE DeleteChars(P,L:Integer);
  81. ---------------------------------------------------------------------------
  82.  
  83.   This  method deletes  L characters   from the  string object  starting at
  84.   position P.
  85.  
  86.   For example:
  87.  
  88.         Var aString : StringObject;
  89.         ...
  90.         begin
  91.                          {         1     }
  92.                          {123456789012345}
  93.           aString.Assign('This is a test.');
  94.           aString.DeleteChars ( 10, 5 );
  95.  
  96.   The above causes the string object to be changed to:
  97.  
  98.           "This is a."
  99.  
  100. ---------------------------------------------------------------------------
  101. PROCEDURE InsertNewChars(P:Integer; newString:String);
  102. ---------------------------------------------------------------------------
  103.  
  104.   This method inserts the newString into the String Object at position P.
  105.  
  106. ---------------------------------------------------------------------------
  107. PROCEDURE MakeUpperCase;
  108. ---------------------------------------------------------------------------
  109.  
  110.   This method turns every character in the String Object into its uppercase
  111.   form.
  112.  
  113. ---------------------------------------------------------------------------
  114. PROCEDURE PadString(L:Byte);
  115. ---------------------------------------------------------------------------
  116.  
  117.   This method changes the length of the  String Object to Length L. If L is
  118.   less than  the current length  of the  string,  it is truncated.  If L is
  119.   greater than the current length of the string, it will be padded out with
  120.   blanks.
  121.  
  122. ---------------------------------------------------------------------------
  123. PROCEDURE ReplaceAll(FromString, ToString:String);
  124. ---------------------------------------------------------------------------
  125.  
  126.   This method finds  all occurrences of the characters  in the "FromString"
  127.   and changes them to the characters in the "ToString".
  128.  
  129. ---------------------------------------------------------------------------
  130. PROCEDURE StripLeadingSpaces;
  131. ---------------------------------------------------------------------------
  132.  
  133.   This method  strips any leading blanks  from the beginning of  the String
  134.   Object.
  135.  
  136. ---------------------------------------------------------------------------
  137. PROCEDURE StripTrailingSpaces;
  138. ---------------------------------------------------------------------------
  139.  
  140.   This method strips any trailing blanks from the end of the String Object.
  141.  
  142. *)
  143.  
  144.  
  145. {-----------------------------------------------------------------------------}
  146.  
  147.                                  INTERFACE
  148.  
  149. {-----------------------------------------------------------------------------}
  150.  
  151. TYPE
  152.  
  153.  
  154.   StringObjectPointer        = ^StringObject;
  155.  
  156.  
  157.   StringObject =
  158.  
  159.   OBJECT                                                 { String object type }
  160.  
  161.     St                       : String;
  162.  
  163.     PROCEDURE Assign(S:String);
  164.     PROCEDURE PadString(L:Byte);
  165.     PROCEDURE InsertNewChars(P:Integer; newString:String);
  166.     PROCEDURE DeleteChars(P,L:Integer);
  167.     PROCEDURE MakeUpperCase;
  168.     PROCEDURE StripLeadingSpaces;
  169.     PROCEDURE StripTrailingSpaces;
  170.     PROCEDURE ReplaceAll(FromString, ToString:String);
  171.     PROCEDURE ConstructStringOf(C:Char; N:Integer);
  172.  
  173.     FUNCTION CountOccurencesOfChar(CaseInsensitive:Boolean;C:Char):Integer;
  174.     FUNCTION Len:Integer;
  175.     FUNCTION Show:String;
  176.     FUNCTION PositionWhere(aString2:String):Integer;
  177.  
  178.   END;                                                      { of StringObject }
  179.  
  180. {-----------------------------------------------------------------------------}
  181.  
  182.                                 IMPLEMENTATION
  183.  
  184. {-----------------------------------------------------------------------------}
  185.  
  186.  
  187. {=============================================================================}
  188. { Assign                                                                      }
  189. {-----------------------------------------------------------------------------}
  190. PROCEDURE StringObject.Assign(S : String);
  191.  
  192. BEGIN
  193.  
  194.   St := S;
  195.  
  196. END;                                                                 { Assign }
  197. {=============================================================================}
  198.  
  199. {=============================================================================}
  200. { Len                                                                         }
  201. {-----------------------------------------------------------------------------}
  202. FUNCTION StringObject.Len  : Integer;
  203.  
  204. BEGIN
  205.  
  206.   Len := Length(St);
  207.  
  208. END;                                                                    { Len }
  209. {=============================================================================}
  210.  
  211. {=============================================================================}
  212. { Show                                                                        }
  213. {-----------------------------------------------------------------------------}
  214. FUNCTION StringObject.Show : String;
  215.  
  216. BEGIN
  217.  
  218.   Show := St;
  219.  
  220. END;                                                                  { Show  }
  221. {=============================================================================}
  222.  
  223. {=============================================================================}
  224. { PadString                                                                   }
  225. {-----------------------------------------------------------------------------}
  226. PROCEDURE StringObject.PadString(L : Byte);
  227.  {- Pad St out to Len with blanks/spaces }
  228.  
  229. VAR
  230.   OldLength                  : Byte;
  231.  
  232. BEGIN
  233.  
  234.   {Save the old lenght of the String}
  235.   OldLength := StringObject.Len;
  236.  
  237.   {Force the length of the string to be the value of L.}
  238.   {$R-}
  239.     St[0] := Chr(L);
  240.   {$R+}
  241.  
  242.   {Pad the string with blanks if the string is being enlarged.}
  243.   IF OldLength < L THEN FillChar(St[OldLength + 1], L - OldLength, ' ');
  244.  
  245. END;                                                              { PadString }
  246. {=============================================================================}
  247.  
  248. {=============================================================================}
  249. { InsertNewChars                                                                 }
  250. {-----------------------------------------------------------------------------}
  251. PROCEDURE StringObject.InsertNewChars(P : Integer; newString : String);
  252.  
  253. VAR
  254.   aString                    : String;
  255.   I                          : Integer;
  256.  
  257. BEGIN
  258.  
  259.   IF Length(newString) = 0 THEN Exit;
  260.   aString := '';
  261.   FOR I := 1 TO P - 1 DO aString := aString + St[I];
  262.   aString := aString + newString;
  263.   FOR I := P TO (Length(St)) DO aString := aString + St[I];
  264.   St := aString;
  265.  
  266. END;                                                         { InsertNewChars }
  267. {=============================================================================}
  268.  
  269. {=============================================================================}
  270. { DeleteChars                                                                 }
  271. {-----------------------------------------------------------------------------}
  272. PROCEDURE StringObject.DeleteChars(P, L : Integer);
  273.  
  274. VAR
  275.   aString                    : String;
  276.   I                          : Integer;
  277.  
  278. BEGIN
  279.  
  280.   IF Length(St) = 0 THEN Exit;
  281.   aString := '';
  282.   FOR I := 1 TO P - 1 DO aString := aString + St[I];
  283.   FOR I := P + L TO Length(St) DO aString := aString + St[I];
  284.   St := aString;
  285.  
  286. END;                                                            { DeleteChars }
  287. {=============================================================================}
  288.  
  289. {=============================================================================}
  290. { MakeUpperCase                                                                   }
  291. {-----------------------------------------------------------------------------}
  292. PROCEDURE StringObject.MakeUpperCase;
  293.  
  294. VAR I                      : Integer;
  295.  
  296. BEGIN
  297.  
  298.   FOR I := 1 TO Length(St) DO St[I] := Upcase(St[I]);
  299.  
  300. END;                                                          { MakeUpperCase }
  301. {=============================================================================}
  302.  
  303. {=============================================================================}
  304. { Strip Leading Spaces                                                        }
  305. {-----------------------------------------------------------------------------}
  306. PROCEDURE StringObject.StripLeadingSpaces;
  307.  
  308. VAR
  309.   StLen                      : Byte;
  310.  
  311. BEGIN
  312.  
  313.    StLen := Byte(St[0]);
  314.  
  315.    WHILE (St[1] = ' ') AND (StLen > 0) DO BEGIN
  316.      Move(St[2], St[1], Pred(StLen));
  317.      StLen := Pred(StLen);
  318.      St[0] := Chr(StLen);
  319.    END;
  320.  
  321. END;                                                     { StripLeadingSpaces }
  322. {=============================================================================}
  323.  
  324. {=============================================================================}
  325. {
  326. {-----------------------------------------------------------------------------}
  327. PROCEDURE StringObject.StripTrailingSpaces;
  328.  
  329. BEGIN
  330.  
  331.   WHILE (St[Length(St)] = ' ') AND (Length(St) > 0) DO St[0] := Pred(St[0]);
  332.  
  333. END;                                                    { StripTrailingSpaces }
  334. {=============================================================================}
  335.  
  336. {=============================================================================}
  337. { Count Char                                                                  }
  338. {-----------------------------------------------------------------------------}
  339. FUNCTION StringObject.CountOccurencesOfChar(CaseInsensitive : Boolean; C : Char) : Integer;
  340.  
  341. VAR
  342.   I                          : Integer;
  343.   Count                      : Integer;
  344.   S                          : String;
  345.  
  346. BEGIN
  347.  
  348.   S := St;
  349.   IF CaseInsensitive THEN FOR I := 1 TO Length(St) DO S[I] := Upcase(St[I]);
  350.   Count := 0;
  351.   FOR I := 1 TO Length(S) DO IF S[I] = C THEN Inc(Count);
  352.   CountOccurencesOfChar := Count;
  353.  
  354. END;                                                  { CountOccurencesOfChar }
  355. {=============================================================================}
  356.  
  357. {=============================================================================}
  358. { Replace All                                                                 }
  359. {-----------------------------------------------------------------------------}
  360. PROCEDURE StringObject.ReplaceAll(FromString, ToString : String);
  361.  
  362. VAR
  363.   Posn                       : Integer;
  364.   Match                      : Boolean;
  365.   I                          : Integer;
  366.   SaveSt                     : String;
  367.  
  368. BEGIN
  369.  
  370.   { Find all short cuts}
  371.  
  372.   IF Length(St) = 0 THEN Exit;
  373.   IF Length(FromString)>Length(St) then Exit;
  374.   If Length(FromString)=0 then Exit;
  375.  
  376.   If St=FromString then begin
  377.     St:=ToString;
  378.     Exit;
  379.   end;
  380.  
  381.   SaveSt := St;
  382.   Posn := 0;
  383.  
  384.   REPEAT
  385.  
  386.     Match := True;
  387.     Inc(Posn);
  388.  
  389.     FOR I := 1 TO Length(FromString) DO BEGIN
  390.       IF (Posn + I - 1) > 255 THEN BEGIN
  391.         St := SaveSt;
  392.         Exit;
  393.       END;
  394.       IF St[Posn + I - 1] <> FromString[I] THEN Match := False;
  395.     END;
  396.  
  397.     IF Match THEN BEGIN
  398.       DeleteChars(Posn, Length(FromString));
  399.       InsertNewChars(Posn, ToString);
  400.       Posn := Posn + Length(ToString) - 1;
  401.     END;
  402.  
  403.   UNTIL Posn = Length(St);
  404.  
  405. END;                                                             { ReplaceAll }
  406. {=============================================================================}
  407.  
  408. {=============================================================================}
  409. { ConstructStringOf                                                             }
  410. {-----------------------------------------------------------------------------}
  411. PROCEDURE StringObject.ConstructStringOf(C : Char; N : Integer);
  412.  
  413. VAR
  414.   S                          : String;
  415.  
  416. BEGIN
  417.  
  418.   IF N < 0 THEN N := 0;
  419.   S[0] := Chr(N);
  420.   FillChar(S[1], N, C);
  421.   St := S;
  422.  
  423. END;                                                      { ConstructStringOf }
  424. {=============================================================================}
  425.  
  426. {=============================================================================}
  427. { Position Where                                                              }
  428. {-----------------------------------------------------------------------------}
  429. FUNCTION StringObject.PositionWhere(aString2 : String) : Integer;
  430.  
  431. VAR
  432.   aString                    : String;
  433.  
  434.   FUNCTION UC(S : String) : String;
  435.  
  436.   VAR
  437.     I                          : Integer;
  438.     aString                    : String;
  439.  
  440.   BEGIN
  441.     aString[0] := S[0];
  442.     FOR I := 1 TO Length(S) DO aString[I] := Upcase(S[I]);
  443.     UC := aString;
  444.   END;
  445.  
  446. BEGIN
  447.  
  448.   aString := UC(St);
  449.   aString2 := UC(aString2);
  450.   PositionWhere := Pos(aString2, aString);
  451.  
  452. END;                                                          { PositionWhere }
  453. {=============================================================================}
  454.  
  455. {=============================================================================}
  456. BEGIN
  457.  
  458.   { Initialization }
  459.  
  460. END.
  461. {=============================================================================}
  462.